/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.biz.service.impl;

import me.zhengjie.modules.biz.domain.LabUseRecord;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.biz.repository.LabUseRecordRepository;
import me.zhengjie.modules.biz.service.LabUseRecordService;
import me.zhengjie.modules.biz.service.dto.LabUseRecordDto;
import me.zhengjie.modules.biz.service.dto.LabUseRecordQueryCriteria;
import me.zhengjie.modules.biz.service.mapstruct.LabUseRecordMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author Admin
* @date 2023-02-17
**/
@Service
@RequiredArgsConstructor
public class LabUseRecordServiceImpl implements LabUseRecordService {

    private final LabUseRecordRepository labUseRecordRepository;
    private final LabUseRecordMapper labUseRecordMapper;

    @Override
    public Map<String,Object> queryAll(LabUseRecordQueryCriteria criteria, Pageable pageable){
        Page<LabUseRecord> page = labUseRecordRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(labUseRecordMapper::toDto));
    }

    @Override
    public List<LabUseRecordDto> queryAll(LabUseRecordQueryCriteria criteria){
        return labUseRecordMapper.toDto(labUseRecordRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public LabUseRecordDto findById(Integer id) {
        LabUseRecord labUseRecord = labUseRecordRepository.findById(id).orElseGet(LabUseRecord::new);
        ValidationUtil.isNull(labUseRecord.getId(),"LabUseRecord","id",id);
        return labUseRecordMapper.toDto(labUseRecord);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public LabUseRecordDto create(LabUseRecord resources) {
        return labUseRecordMapper.toDto(labUseRecordRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(LabUseRecord resources) {
        LabUseRecord labUseRecord = labUseRecordRepository.findById(resources.getId()).orElseGet(LabUseRecord::new);
        ValidationUtil.isNull( labUseRecord.getId(),"LabUseRecord","id",resources.getId());
        labUseRecord.copy(resources);
        labUseRecordRepository.save(labUseRecord);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            labUseRecordRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<LabUseRecordDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (LabUseRecordDto labUseRecord : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("实验室", labUseRecord.getLabId());
            map.put("使用者", labUseRecord.getUserId());
            map.put("预约开始时点", labUseRecord.getStartAt());
            map.put("预约结束时间点", labUseRecord.getEndAt());
            map.put("创建时间", labUseRecord.getCreatedAt());
            map.put("修改时间", labUseRecord.getUpdatedAt());
            map.put("是否删除", labUseRecord.getIsDeleted());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}